package menu.lt.pers.menu.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import menu.lt.pers.menu.Bean.HistoryBean;
import menu.lt.pers.menu.Bean.ResultByNameBean;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.R;
import menu.lt.pers.menu.RecipeInfo;
import menu.lt.pers.menu.dao.HistoryDao;

import java.util.ArrayList;
import java.util.List;

public class HistoryRecyclerViewAdapter extends RecyclerView.Adapter<HistoryRecyclerViewAdapter.MyHolder>{
    private Context context;
    private List<HistoryBean> list;
    private Gson gson;
    private List<ResultByNameBean.ResultBean.ListBean.MaterialBean> materials;
    private List<ResultByNameBean.ResultBean.ListBean.ProcessBean> processes;
    private HistoryDao historyDao;
    private Cursor cursor;
    private UserBean user;

    public HistoryRecyclerViewAdapter(Context context, UserBean user) {
        this.context = context;
        this.list = new ArrayList<>();
        this.historyDao = new HistoryDao(context);
        this.user = user;
        gson = new Gson();
        GetData();
    }

    private void GetData() {
        cursor = historyDao.findByUserId(user);
        if(cursor.moveToFirst()){
            list.clear();
            do {
                HistoryBean history = new HistoryBean();
                int _id = cursor.getInt(0);
                int class_id = cursor.getInt(1);
                String name = cursor.getString(2);
                String peoplenum = cursor.getString(3);
                String preparetime = cursor.getString(4);
                String cookingtime = cursor.getString(5);
                String content = cursor.getString(6);
                String pic = cursor.getString(7);
                String tag = cursor.getString(8);
                String materialJson = cursor.getString(9);
                String processJSon = cursor.getString(10);
                int user_id = cursor.getInt(11);
                String time = cursor.getString(12);

                history.set_id(_id);
                history.setClassid(class_id);
                history.setName(name);
                history.setPeoplenum(peoplenum);
                history.setPreparetime(preparetime);
                history.setCookingtime(cookingtime);
                history.setContent(content);
                history.setPic(pic);
                history.setTag(tag);
                history.setMaterialJson(materialJson);
                history.setProcessJson(processJSon);
                history.setUserid(user_id);
                history.setTime(time);
                Log.i("Fragment_my.btn_collection.setOnClickListener",history.getName());
                list.add(history);
            }while(cursor.moveToNext());
            cursor.close();
        }
    }

    public Context getContext() {
        return context;
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }
    public List<HistoryBean> getList() {
        return list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.activity_recipe_info_listview_material_resource,viewGroup,false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.tv_name.setText(list.get(i).getName());
        myHolder.tv_time.setText(list.get(i).getTime());

        final int index = i;
        materials = gson.fromJson(list.get(i).getMaterialJson(),
                new TypeToken<ArrayList<ResultByNameBean.ResultBean.ListBean.MaterialBean>>() {}.getType());
        processes = gson.fromJson(list.get(i).getProcessJson(),
                new TypeToken<ArrayList<ResultByNameBean.ResultBean.ListBean.ProcessBean>>() {}.getType());
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResultByNameBean.ResultBean.ListBean recipe = new ResultByNameBean.ResultBean.ListBean();
                recipe.setMaterial(materials);
                recipe.setProcess(processes);
                recipe.setId(list.get(index).get_id());
                recipe.setClassid(list.get(index).getClassid());
                recipe.setContent(list.get(index).getContent());
                recipe.setCookingtime(list.get(index).getCookingtime());
                recipe.setName(list.get(index).getName());
                recipe.setPeoplenum(list.get(index).getPeoplenum());
                recipe.setPreparetime(list.get(index).getPreparetime());
                recipe.setPic(list.get(index).getPic());
                recipe.setTag(list.get(index).getTag());
                Intent intent = new Intent(context, RecipeInfo.class);
                intent.putExtra("recipe",gson.toJson(recipe));
                Log.i("itemView.setOnClickListener", "onClick: "+"准备跳转");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_time;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_material_name);
            tv_time = itemView.findViewById(R.id.tv_material_num);
        }
    }

}
